do "E:\yungyu\lottery_baby\do\set_environment.do"

clear
set more off

global control1 "i.pers_gender i.houhld_resid_cd pers_income_pre1 pers_asset_pre1 pers_earnings_pre1 pers_labor_pre1 married_pre1"
global control2 "totbaby_pre1"
global control3 "pre*redeem*"

use "$wdata\project_lottery_fertility_b3a6_first_5k.dta", clear
	
drop if prize10M > 5

gen tot_asset = pers_asset + spouse_asset
	
gen anybaby_pre1 = totbaby_pre1 > 0
gen single_pre1 = married_pre1 == 0
gen singleanybaby_pre1 = totbaby_pre1 > 0 if married_pre1 == 0
gen coupleanybaby_pre1 = totbaby_pre1 > 0 if married_pre1 == 1

replace norm_year = norm_year + 3
	
cap rm "$table/temp/Tab5.txt"
cap rm "$table/temp/Tab5.xls"

foreach y in totbaby { 
	foreach grp in anybaby single coupleanybaby{
		forv j = 0(1)1{
			
			sum `y' if current == 0 & norm == 2 & `grp'_pre1 == `j'
			loc m0 = r(mean)
			sum `y' if current == 0 & norm == 9 & `grp'_pre1 == `j'
			loc m1 = r(mean)
			loc bt = `m1' - `m0'
			loc bt: dis %9.3f `bt'
			
			sum tot_asset if current == 0 & norm == 2 & `grp'_pre1 == `j'
			loc m0t = r(mean)
			sum tot_asset if current == 0 & norm == 9 & `grp'_pre1 == `j'
			loc m1t = r(mean)
			loc bt_wt = `m1t' - `m0t' //Baseline trend in wealth
			loc m0t: dis %9.3f `m0t'
			loc bt_wt: dis %9.3f `bt_wt'
			
			reghdfe `y' currentxprizexp* prize10M c.prize10M#i.norm c.prize10M#1.current $control1 $control2 $control3 if `grp'_pre1 == `j', a(norm#current year age) cl(houseid)
			outreg2 using "$table/temp/Tab5.xls" , append bd(3) sd(3) nocon keep(currentxprize*) ctitle(`grp'`j') addtext(Baseline Trend, `bt', Baseline Wealth, `m0t', Baseline Trend in Wealth, `bt_wt')
		}
	}
}


import delimited using "$table/temp/Tab5.txt", clear

forv i = 2(1)7{
    replace v`i' = "" in 2
}

replace v2 = "Parenthood Status" in 2
replace v5 = "Household Status" in 2

replace v2 = "w/o Child" in 3
replace v3 = "w/ Child" in 3
replace v5 = "Single" in 3
replace v4 = "Couple" in 3
order v5, b(v4)
replace v6 = "Couple w/o Child" in 3
replace v7 = "Couple w/ Child" in 3

merge 1:1 _n using "$table/temp/Tab5-I.dta"
drop _m

order v1 v2 v3 vv2 v5 v4 vv3 v6 v7 vv4

forv i = 2(1)4{
	replace vv`i' = "Difference" in 3
}

drop vv1

export excel "$table/Tables.xlsx", sheet("Tab5") sheetreplace
